package com.itheima.behaviour.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.behaviour.dto.FollowBehaviourDto;
import com.itheima.behaviour.mapper.ApBehaviorEntryMapper;
import com.itheima.behaviour.mapper.ApFollowBehaviorMapper;
import com.itheima.behaviour.pojo.ApBehaviorEntry;
import com.itheima.behaviour.pojo.ApFollowBehavior;
import com.itheima.behaviour.service.ApFollowBehaviorService;
import com.itheima.common.constants.SystemConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @description <p>APP关注行为 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.behaviour.service.impl
 */
@Service
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements ApFollowBehaviorService {

    @Autowired
    private ApBehaviorEntryMapper apBehaviorEntryMapper;
    /**
     * 保存用户行为
     *
     * @param followBehaviourDto
     */
    //这里要判断是否是注册用户还是路人，如果是注册用户就保存用户的行为
    @Override
    public void saveFollowBehaviour(FollowBehaviourDto followBehaviourDto) {
        //查询是否有用户实体，没有不处理，有就创建用户行为
        ApBehaviorEntry entry = apBehaviorEntryMapper
                .findByEntryIdAndType(followBehaviourDto.getUserId(), SystemConstants.TYPE_USER);
        if(null != entry){
            ApFollowBehavior pojo = new ApFollowBehavior();
            pojo.setArticleId(followBehaviourDto.getArticleId());
            pojo.setEntryId(entry.getId());
            pojo.setFollowId(followBehaviourDto.getFollowId());
            pojo.setCreatedTime(LocalDateTime.now());
            save(pojo);
        }

    }
}
